package com.thread;

import java.util.Random;

public class TestThreadlocal {
    static ThreadLocal<Student> threadLocalst=new ThreadLocal<Student>();
    //static Student st=new Student();
    public  static  void  main(String [] args)
    {
        //System.out.print("asdf");
/*        Student st=new Student();
        SetThread r1=new SetThread(st);
        GetThread r2=new GetThread(st);
        Thread thread1=new Thread(r1,"添加");
        Thread thread2=new Thread(r2,"取出");
        thread1.start();
        thread2.start();*/

/*        MyConnection myConnection=MyConnection.getMyconnection();

        MyConnection2 myConnection2=MyConnection2.getConnection();*/
        for (int i=0;i<10;i++)
        {
            new Thread(new Runnable() {
                public void run() {
                    Student student=new Student();
                    student.setAge(new Random().nextInt() +"");
                    student.setName("xcy");
                    System.out.println(Thread.currentThread().getName()+ student.getName()+student.getAge());
                    threadLocalst.set(student);

/*                    st.setAge(new Random().nextInt() +"");
                    st.setName("xcy");
                    System.out.println(Thread.currentThread().getName()+ st.getName()+st.getAge());*/

                    new A().get();
                    new B().get();

                }
            }){}.start();
        }

    }
    static class A{
        public void get (){
            //System.out.println(Thread.currentThread().getName()+ st.getName()+st.getAge());
            Student st=threadLocalst.get();
            System.out.println(Thread.currentThread().getName()+ st.getName()+st.getAge());

        }
    }
    static class B{
        public void get (){
            //System.out.println(Thread.currentThread().getName()+ st.getName()+st.getAge());
            Student st=threadLocalst.get();
            System.out.println(Thread.currentThread().getName()+ st.getName()+st.getAge());

        }
    }
/*    public static void getlocal()
    {
        Student student= threadLocalst.get();
        System.out.println(Thread.currentThread().getName()+ student.getName()+student.getAge());

    }*/
}
